package com.yuandian.customer.perception.controller;

import com.yuandian.api.customer_perception.po.BasePortraitParams;
import com.yuandian.api.customer_perception.vo.MatterProcessDetailVO;
import com.yuandian.api.customer_perception.vo.MatterProcessOverrideVO;
import com.yuandian.api.customer_perception.vo.MatterProcessStatisticsVO;
import com.yuandian.api.customer_perception.vo.UserOpertorStepOverrideVO;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.service.IBasePortraitService;
import com.yuandian.utils.poi.ExcelTableInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liXin
 * @description 单位画像
 * @date 2022/4/24
 */
@Slf4j
@RestController
@Api(value = "单位/用户画像", tags = "单位/用户画像")
@RequestMapping("/basePortrait")
public class BasePortraitController {

    @Autowired
	IBasePortraitService basePortraitService;

    /**
     *  事项办理概览
     *  默认按照成功率降序排序
     * @return
     */
    @PostMapping("queryPortraitOverview")
    @ApiOperation(value = "事项办理概览", notes = "事项办理概览")
    public R<Object> queryPortraitOverview(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            List<String> menuIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                String menuId=s.split("@")[1];
                matterIds.add(matterId);
                menuIds.add(menuId);
            });
            params.setMenuId(menuIds);
            params.setMatterIds(matterIds);
        }
        return basePortraitService.queryPortraitOverview(params);
    }

    /**
     *  事项办理概览导出
     * @return
     */
    @PostMapping("queryPortraitOverviewExport")
    @ApiOperation(value = "事项办理概览导出", notes = "事项办理概览导出")
    public void queryPortraitOverviewExport(@RequestBody BasePortraitParams params, HttpServletResponse response) throws IOException {
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        params.setPageNum(1L);
        params.setPageSize(-1);

        // 2、导出Excel
        String title = "事项办理概览";
        List<String> titleParams = new ArrayList<>();
        titleParams.add(title);
//        titleParams.addAll(params.getSystemNames());
        List<String> headList = Arrays.asList("序号",
                "地市", "单位编码","单位名称", "用户id", "用户姓名", "系统名称", "事项办理数量", "访问量", "告警量","失败量",
                "系统平均处理时长", "页面平均停留时长","成功率");
        new ExcelTableInfo.Builder(params, MatterProcessOverrideVO.class)
                .setNumberName("id")
                .setTitle(titleParams)
                .setHeader(headList)
                .setTimeFormatFields("avgFreeTime","avgSystemDealTime")
                .setSuffixPercentageFields("successRate")
                .setDataListByResult(queryPortraitOverview(params).getData())
                .setSheetName(title)
                .build().exportExcel(response);
    }


    /**
     *  用户操作步骤概况
     * @return
     */
    @PostMapping("queryUserOperationStepOverview")
    @ApiOperation(value = "用户操作步骤概况", notes = "用户操作步骤概况")
    public R<Object> queryUserOperationStepOverview(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        return basePortraitService.queryUserOperationStepOverview(params);
    }

    /**
     *  用户操作步骤概况导出
     * @return
     */
    @PostMapping("queryUserOperationStepOverviewExport")
    @ApiOperation(value = "用户操作步骤概况导出", notes = "用户操作步骤概况导出")
    public void queryUserOperationStepOverviewExport(@RequestBody BasePortraitParams params, HttpServletResponse response) throws IOException {
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        params.setPageNum(1L);
        params.setPageSize(-1);

        // 2、导出Excel
        String title = "用户操作步骤概况";
        List<String> titleParams = new ArrayList<>();
        titleParams.add(title);
//        titleParams.addAll(params.getSystemNames());

        List<String> headList = Arrays.asList("序号",
                "用户ID", "用户姓名","系统名称","事项名称","步骤名称", "菜单名称", "按钮URL", "系统处理时长", "网络传输时长","告警量",
                "开始时间", "结束时间");
        new ExcelTableInfo.Builder(params, UserOpertorStepOverrideVO.class)
                .setNumberName("id")
                .setTitle(titleParams)
                .setTimeFormatFields("dealTime","transmitTime")
                .setHeader(headList)
                .setDataListByResult(queryUserOperationStepOverview(params).getData())
                .setSheetName(title)
                .build().exportExcel(response);
    }



    /**
     *   办理事项统计
     *  默认按照成功率降序排序
     * @return
     */
    @PostMapping("queryMatterProcessStatistics")
    @ApiOperation(value = "办理事项统计", notes = "办理事项统计")
    public R<Object> queryMatterProcessStatistics(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            List<String> menuIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                String menuId=s.split("@")[1];
                matterIds.add(matterId);
                menuIds.add(menuId);
            });
            params.setMatterIds(matterIds);
            params.setMenuId(menuIds);
        }
        return basePortraitService.queryMatterProcessStatistics(params);
    }

    /**
     *  办理事项统计导出
     * @return
     */
    @PostMapping("queryMatterProcessStatisticsExport")
    @ApiOperation(value = "办理事项统计导出", notes = "办理事项统计导出")
    public void queryMatterProcessStatisticsExport(@RequestBody BasePortraitParams params, HttpServletResponse response) throws IOException {
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        params.setPageNum(1L);
        params.setPageSize(-1);
        // 2、导出Excel
        String title = "办理事项统计";
        List<String> titleParams = new ArrayList<>();
        titleParams.add(title);
//        titleParams.addAll(params.getSystemNames());

        List<String> headList = Arrays.asList("序号",
                "事项名称", "系统名称","办理量", "访问量", "告警量", "失败量", "系统平均处理时长", "页面平均停留时长", "成功率");

        new ExcelTableInfo.Builder(params, MatterProcessStatisticsVO.class)
                .setNumberName("id")
                .setTitle(titleParams)
                .setTimeFormatFields("avgSystemDealTime","avgFreeTime")
                .setSuffixPercentageFields("successRate")
                .setHeader(headList)
                .setDataListByResult(queryMatterProcessStatistics(params).getData())
                .setSheetName(title)
                .build().exportExcel(response);
    }

    /**
     *   办理事项明细
     *
     * @return
     */
    @PostMapping("queryMatterProcessDetail")
    @ApiOperation(value = "办理事项明细", notes = "办理事项明细")
    public R<Object> queryMatterProcessDetail(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        return basePortraitService.queryMatterProcessDetail(params);
    }
    /**
     *   办理事项明细导出
     * @return
     */
    @PostMapping("queryMatterProcessDetailExport")
    @ApiOperation(value = "办理事项明细导出", notes = "办理事项明细导出")
    public void queryMatterProcessDetailExport(@RequestBody BasePortraitParams params, HttpServletResponse response) throws IOException {
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        params.setPageNum(1L);
        params.setPageSize(-1);
        // 2、导出Excel
        String title = "办理事项明细";
        List<String> titleParams = new ArrayList<>();
        titleParams.add(title);
//        titleParams.addAll(params.getSystemNames());

        List<String> headList = Arrays.asList("序号",
                 "系统名称","事项名称", "步骤数", "系统处理时长", "页面停留时长", "网络传输时长", "告警量", "开始时间","结束时间");
        new ExcelTableInfo.Builder(params, MatterProcessDetailVO.class)
                .setNumberName("id")
                .setTitle(titleParams)
                .setHeader(headList)
                .setTimeFormatFields("transmitTime","freeTime","systemDealTime")
                .setDataListByResult(queryMatterProcessDetail(params).getData())
                .setSheetName(title)
                .build().exportExcel(response);
    }

    /**
     *   办理事项步骤明细
     *
     * @return
     */
    @PostMapping("queryMatterProcessStepDetail")
    @ApiOperation(value = "办理事项步骤明细", notes = "办理事项步骤明细")
    public R<Object> queryMatterProcessStepDetail(@RequestBody BasePortraitParams params){
        return basePortraitService.queryMatterProcessStepDetail(params);
    }

    /**
     *  获取事项名称
     * @return
     */
    @PostMapping("queryMatterName")
    @ApiOperation(value = "获取事项名称", notes = "获取事项名称")
    public R<Object> queryMatterName(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        return basePortraitService.queryMatterName(params);
    }

    /**
     * 查询已经被告警的步骤id
     */
    @PostMapping("queryStepIds")
    @ApiOperation(value = "查询已经被告警的步骤id", notes = "查询已经被告警的步骤id")
    public R<Object> queryStepIds(@RequestBody BasePortraitParams params){
        return basePortraitService.queryStepIds(params);
    }


}
